package com.education.basic.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.education.basic.dao.TeachInfoDAO;
import com.education.basic.entity.TeachInfo;
import com.yunshangwoniu.core.baseDao.impl.BaseDaoImpl;
import com.yunshangwoniu.core.utils.Logger;

@Component("teachInfoDAO")
public class TeachInfoDAOImpl extends BaseDaoImpl<TeachInfo> implements TeachInfoDAO
{

	@Override
	public List<TeachInfo> queryAll() 
	{
		Session session = sessionFactory.getCurrentSession();
		String hql = "from TeachInfo";
		Query q = session.createQuery(hql);
		@SuppressWarnings("unchecked")
		List<TeachInfo> list = q.list();
		try
		{
			Logger.debug(this.getClass(), String.format("query all teachInfo query [size:%s]", list.size()));
		}catch(Exception e)
		{
			Logger.warn(this.getClass(), "query all teachInfo query list is null");
		}
		return list;
	}

	@Override
	public TeachInfo getByCourseName(String courseName) 
	{
		Session session = sessionFactory.getCurrentSession();
		String hql = "from TeachInfo t where t.courseName=?";
		Query q  = session.createQuery(hql);
		q.setParameter(0, courseName);
		TeachInfo info = (TeachInfo)q.uniqueResult();
		if(info!=null)
		{
			Logger.debug(this.getClass(), String.format("get teachInfo success"));
			return info;
		}else
		{
			Logger.debug(this.getClass(), String.format("get teachInfo fail it not exist!"));
			return null;
		}
		
	}

    @SuppressWarnings("unchecked")
	@Override
    public List<TeachInfo> getTeachInfosByEmpno(String empno)
    {
        Session session = sessionFactory.getCurrentSession();
        String hql = "";
        List<TeachInfo> list = null;
        if(!"".equals(empno))
        {
            hql = "from TeachInfo t where t.empno=?";
            Query q  = session.createQuery(hql);
            q.setParameter(0, empno);
            list = q.list();
        }else
        {
            hql = "from TeachInfo t";
            Query q  = session.createQuery(hql);
            list = q.list();
        }
        if(list!=null&&list.size()>0)
        {
            Logger.debug(this.getClass(), String.format("get teachInfos success"));
            return list;
        }else
        {
            Logger.debug(this.getClass(), String.format("get teachInfos is empty"));
            return list;
        }
    }

    @Override
    public TeachInfo getByCourseCode(String courseCode)
    {
        Session session = sessionFactory.getCurrentSession();
        String hql = "from TeachInfo t where t.courseCode=?";
        Query q  = session.createQuery(hql);
        q.setParameter(0, courseCode);
        TeachInfo info = (TeachInfo)q.uniqueResult();
        if(info!=null)
        {
            Logger.debug(this.getClass(), String.format("get teachInfo success"));
            return info;
        }else
        {
            Logger.debug(this.getClass(), String.format("get teachInfo fail it not exist!"));
            return null;
        }
    }
}
